package cn.com.tbshuda.shell.shell.jsch;

/**
 * @author: wsd
 * @date: 2021/6/16 15:41
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import cn.com.tbshuda.shell.shell.client.ConnectShell;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

/**
 * This class provide interface to execute command on remote Linux.
 */

public class SSHCommandExecutor {
    private String ipAddress;

    private String username;

    private String password;
    private Session session;

    public static final int DEFAULT_SSH_PORT = 22;


    public SSHCommandExecutor(final String ipAddress, final String username, final String password) {
        this.ipAddress = ipAddress;
        this.username = username;
        this.password = password;
    }

    public int execute( String command) {
        int returnCode = 0;
        JSch jsch = new JSch();
        MyUserInfo userInfo = new MyUserInfo();

        try {
            // Create and connect session.
            if(session==null){
                session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
                session.setPassword(password);
                session.setUserInfo(userInfo);
                session.connect();
            }
            // Create and connect channel.
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            channel.setInputStream(null);
            BufferedReader input = new BufferedReader(new InputStreamReader(channel
                    .getInputStream()));

            channel.connect();
            System.out.println("The remote command is: " + command);

            // Get the output of remote command.
            String line;
            List<String> dd = new ArrayList<>();
            while ((line = input.readLine()) != null) {
                dd.add(line);
                System.out.println(line);
            }
            input.close();

            // Get the return code only after the channel is closed.
            if (channel.isClosed()) {
                returnCode = channel.getExitStatus();
            }

            // Disconnect the channel and session.
            channel.disconnect();
            session.disconnect();
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnCode;
    }


    public static String connIp = "47.93.248.77";
    public static String connUser = "root";
    public static String connPwd = "Shuda1112";
    public static void main(final String [] args) {
        SSHCommandExecutor sshExecutor = new SSHCommandExecutor(connIp, connUser, connPwd);
        Scanner scan = new Scanner(System.in);
        scan.useDelimiter("\n");
        while(scan.hasNext()){
            String exe = scan.next() ;
            sshExecutor.execute(exe);

        }
        scan.close();
    }
}